Add a new filter for sorting.
authorrobertl <robertl@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Tue, 8 Jun 2004 06:29:07 +0000 (06:29 +0000)
committerrobertl <robertl@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Tue, 8 Jun 2004 06:29:07 +0000 (06:29 +0000)
Copyright updates.

gpsbabel/Makefile
gpsbabel/defs.h
gpsbabel/filter_vecs.c
gpsbabel/gpsutil.c
gpsbabel/gpx.c
gpsbabel/mkshort.c
gpsbabel/sort.c [new file with mode: 0644]

index 3608fbcf98974b14a0cb4db73c3358442d4acd03..55f6ea2932374acaa3501b5e45a8a3644424480d 100644 (file)
@@ -21,7 +21,7 @@ FMTS=magproto.o gpx.o geo.o mapsend.o mapsource.o \
        gpilots.o saroute.o navicache.o psitrex.o geoniche.o delgpl.o \
        ozi.o nmea.o text.o html.o palmdoc.o
 
-FILTERS=position.o duplicate.o arcdist.o polygon.o smplrout.o reverse_route.o
+FILTERS=position.o duplicate.o arcdist.o polygon.o smplrout.o reverse_route.o sort.o
 
 JEEPS=jeeps/gpsapp.o jeeps/gpscom.o \
        jeeps/gpsmath.o jeeps/gpsmem.o  \
index 974e9dc1f7858ccfc87c37f8a7e360d4ecd5174e..3fbe48bb367ee8cc6133e23447fdbbc6d802dafa 100644 (file)
@@ -118,6 +118,7 @@ typedef struct {
 typedef struct {
        geocache_type type;
        geocache_container container;
+       int id; /* The decimal cache number */
        int diff; /* (multiplied by ten internally) */
        int terr; /* (likewise) */
        time_t exported;
index 6453dc8de66f68aa611c2502d420d957f2fa161c..772d7adf5cdb5672c30dcc31bb8d13241de40d10 100644 (file)
@@ -1,7 +1,7 @@
 /*
     Describe vectors containing filter operations.
  
-    Copyright (C) 2002 Robert Lipe, robertlipe@usa.net
+    Copyright (C) 2002,2004 Robert Lipe, robertlipe@usa.net
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -35,6 +35,7 @@ extern filter_vecs_t arcdist_vecs;
 extern filter_vecs_t polygon_vecs;
 extern filter_vecs_t routesimple_vecs;
 extern filter_vecs_t reverse_route_vecs;
+extern filter_vecs_t sort_vecs;
 
 static
 fl_vecs_t filter_vec_list[] = {
@@ -73,6 +74,11 @@ fl_vecs_t filter_vec_list[] = {
                "reverse",
                "Reverse stops within routes",
        },
+       {
+               &sort_vecs,
+               "sort",
+               "Rearrange waypoints by resorting",
+       },
         {
                NULL,
                NULL,
index 2ab25528c33b58eaf65d7f4e04e69bbdf2da8c36..21ffe1ba643ba5bdf2cdb0347924c3a45855e15a 100644 (file)
@@ -1,7 +1,23 @@
 /*
- * written by robertlipe@usa.net
- */
+    Access gpsutil files.
+
+    Copyright (C) 2002, 2003, 2004 Robert Lipe, robertlipe@usa.net
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
 
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
+
+ */
 #include "defs.h"
 #include "magellan.h"
 
@@ -93,6 +109,7 @@ gpsutil_disp(const waypoint *wpt)
 {
        double lon,lat;
        const char *icon_token;
+       char *tdesc = str_utf8_to_ascii(wpt->description);
 
        icon_token = mag_find_token_from_descr(wpt->icon_descr);
 
@@ -109,8 +126,10 @@ gpsutil_disp(const waypoint *wpt)
                lon < 0.0 ? 'W' : 'E',
                wpt->altitude,
                'm', 
-               wpt->description ? wpt->description : "",
+               wpt->description ? tdesc : "",
                icon_token);
+
+       xfree(tdesc);
 }
 
 static void
index 9d2dc32461c68e5aeeb4c09c901ee505307e2fbf..871aa8fc5ce7ee00cf2e4b78e44d0c26c59c694d 100644 (file)
@@ -77,6 +77,7 @@ typedef enum {
        tt_wpt_time,
        tt_wpt_type,
        tt_wpt_urlname,
+       tt_cache,
        tt_cache_name,
        tt_cache_container,
        tt_cache_type,
@@ -145,6 +146,7 @@ tag_mapping tag_path_map[] = {
        { tt_wpt_urlname, 0, "/gpx/wpt/urlname" },
        { tt_wpt_sym, 0, "/gpx/wpt/sym" },
        { tt_wpt_type, 1, "/gpx/wpt/type" },
+       { tt_cache, 1, "/gpx/wpt/groundspeak:cache" },
        { tt_cache_name, 1, "/gpx/wpt/groundspeak:cache/groundspeak:name" },
        { tt_cache_container, 1, "/gpx/wpt/groundspeak:cache/groundspeak:container" },
        { tt_cache_type, 1, "/gpx/wpt/groundspeak:cache/groundspeak:type" },
@@ -274,6 +276,19 @@ tag_cache_desc(const char ** attrv)
        }
 }
 
+static void
+tag_gs_cache(const char **attrv)
+{
+       const char **avp;
+
+       cache_descr_is_html = 0;
+       for (avp = &attrv[0]; *avp; avp+=2) {
+               if (strcmp(avp[0], "id") == 0) {
+                               wpt_tmp->gc_data.id = atoi(avp[1]);
+               }
+       }
+}
+
 static void
 start_something_else(const char *el, const char **attrv)
 {
@@ -427,6 +442,9 @@ gpx_start(void *data, const char *el, const char **attr)
        case tt_unknown:
                start_something_else(el, attr);
                return;
+       case tt_cache:
+               tag_gs_cache(attr);
+               break;
        case tt_cache_log_wpt:
                if (opt_logpoint)
                        tag_log_wpt(attr);
index 7b653eb27c09d0620565f736eb87c8a7482c4884..15d69bceaff009c878efc7a3c0a22921a396803f 100644 (file)
@@ -1,3 +1,24 @@
+/*
+    Generate unique short names.
+
+    Copyright (C) 2003, 2004 Robert Lipe, robertlipe@usa.net
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
+
+ */
+
 #include <string.h>
 #include <ctype.h>
 #include <stdlib.h>
diff --git a/gpsbabel/sort.c b/gpsbabel/sort.c
new file mode 100644 (file)
index 0000000..f84529a
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+    Arbitrary Sorting Filter(s)
+
+    Copyright (C) 2004 Robert Lipe, robertlipe@usa.net
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
+
+ */
+#include "defs.h"
+
+extern queue waypt_head;
+
+typedef enum {
+       sm_unknown = 0,
+       sm_gcid,
+       sm_shortname,
+       sm_description
+} sort_mode_;
+
+sort_mode_ sort_mode = sm_shortname;   /* How are we sorting these? */
+
+static char *opt_sm_gcid, *opt_sm_shortname, *opt_sm_description;
+
+static
+arglist_t sort_args[] = {
+       {"gcid", &opt_sm_gcid, "Sort by numeric geocache ID", ARGTYPE_BOOL },
+       {"shortname", &opt_sm_shortname, "Sort by waypoint short name", ARGTYPE_BOOL },
+       {"description", &opt_sm_description, "Sort by waypoint description", ARGTYPE_BOOL },
+       {0, 0, 0, 0}
+};
+
+static int
+sort_comp(const void * a, const void * b)
+{
+       const waypoint *x1 = *(waypoint **)a;
+       const waypoint *x2 = *(waypoint **)b;
+
+       switch (sort_mode)  {
+          case sm_gcid: return x1->gc_data.id > x2->gc_data.id;
+          case sm_shortname: return strcmp (x1->shortname, x2->shortname);
+          case sm_description: return strcmp (x1->description, x2->description);
+       }
+}
+
+void 
+sort_process(void)
+{
+       queue * elem, * tmp;
+       waypoint ** comp;
+       int i = 0, wc;
+
+       wc = waypt_count();
+
+       comp = xcalloc(wc, sizeof(*comp));
+
+       QUEUE_FOR_EACH(&waypt_head, elem, tmp) {
+               comp[i] = (waypoint *)elem;
+               waypt_del(comp[i]); /* Pop this waypoint off the master Q */
+               i++;
+       }
+
+       qsort(comp, wc, sizeof(waypoint *), sort_comp);
+
+       /*
+        * Now re-add the list back.
+        */
+       for (i = 0; i < wc ; i++) {
+               waypt_add(comp[i]);
+       }
+
+       if (comp)
+               xfree(comp);
+}
+
+void
+sort_init(const char *args) 
+{
+       if (opt_sm_gcid)
+               sort_mode = sm_gcid;
+       if (opt_sm_shortname)
+               sort_mode = sm_shortname;
+       if (opt_sm_description)
+               sort_mode = sm_description;
+}
+
+filter_vecs_t sort_vecs = {
+       sort_init,
+       sort_process,
+       NULL,
+       sort_args
+};